/*!
 * xnxf 0.1.1 (http://www.wdssmq.com)
 * Copyright 2013-2015 沉冰浮水
 */

module.exports = function (grunt) {

  require('load-grunt-parent-tasks')(grunt, {
    config: 'package.json',
    pattern: 'grunt-*',
    scope: 'dependencies',
    module: 'grunt-collection'
  });

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    path: '<%= pkg.path %>/<%= pkg.name%>',
    banner: '/*!\n' +
        ' * <%= pkg.name%> <%= pkg.version %> (<%= pkg.homepage %>)\n' +
        ' * Copyright 2013-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
        ' * <%= pkg.description %>\n' +
        ' */\n',

    // Task configuration.
    clean: {
      dist: ['dist']
    },

    // 编译
    less: {
      compile: {
        files: {
          '<%= pkg.style %>': '<%= pkg.less %>'
        }
      }
    },

    stylus: {
      compile: {
        files: {
          '<%= pkg.style %>': ['less/style.styl']
        }
      }
    },

    // 压缩
    cssmin: {
      options: {
        keepSpecialComments: '*',
        noAdvanced: true
      },
      core: {
        files: {
          '<%= pkg.styleMin %>': '<%= pkg.style %>'
        }
      }
    },

    // 代码完善
    autoprefixer: {
      options: {
        browsers: [
          'Android >= 4',
          'Chrome >= 20',
          'Firefox >= 24', // Firefox 24 is the latest ESR
          'Explorer >= 9',
          'iOS >= 6',
          'Opera >= 16',
          'Safari >= 6'
        ]
      },
      core: {
        options: {
          map: false
        },
        src: '<%= pkg.style %>'
      }
    },

    // 属性排序
    csscomb: {
      options: {
        config: 'less/.csscomb.json'
      },
      dist: {
        files: {
          '<%= pkg.style %>': '<%= pkg.style %>'
        }
      }
    },

    // 语法检查
    csslint: {
      options: {
        csslintrc: 'less/.csslintrc'
      },
      dist: [
        '<%= pkg.style %>'
      ]
    },

    // 拼接JS
    concat: {
      options: {
        // banner: '<%= banner %>',
        stripBanners: false,
        separator: ';'
      },
      dist: {
        src: ['js/**/*.js'],
        dest: 'dist/common.js'
      }
    },

    // JS压缩
    uglify: {
      options: {
        banner: '<%= banner %>'
      },
      dist: {
        files: {
          'dist/common.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },

    // JS语法检测
    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
      src: {
        src: 'js/*.js'
      }
    },

    // JS书写格式检测，主要是为了让代码好看
    jscs: {
      options: {
        config: 'js/.jscsrc'
      },
      grunt: {
        options: {
          requireCamelCaseOrUpperCaseIdentifiers: null
        },
        src: 'Gruntfile.js'
      },
      src: {
        src: '<%= jshint.src.src %>'
      }
    },

    usebanner: {
      dist: {
        options: {
          position: 'top',
          banner: '<%= banner %>'
        },
        files: {
          src: ['dist/*.css','dist/*.js']
        }
      }
    },

    copy: {
      css: {
        expand: true,
        flatten: true,
        cwd: 'dist/',
        src: '*.css',
        dest: '<%= path %>/STYLE/'
      },
      js: {
        expand: true,
        flatten: true,
        cwd: 'dist/',
        src: '*.min.js',
        dest: '<%= path %>/script/'
      },
      s: {
        files: [
          {
            expand: true,
            src: ['package.json'],
            dest: '<%= grunt.template.today("yyyy-mm-dd") %>'
          },
          {
            expand: true,
            cwd: 's/',
            src: ['**/*','**/.*'],
            dest: '<%= grunt.template.today("yyyy-mm-dd") %>'
          }
        ]
      }
    },

    watch: {
      // 自动编译
      scripts: {
        files: ['less/**/*.styl'],
        tasks: ['dist-css','dist-js']
      },
      // 自动刷新网页
      client: {
        files: ['<%= path %>/**/*.css','<%= path %>/**/*.html','<%= path %>/**/*.php','<%= path %>/**/*.asp','<%= path %>/**/*.js'],
        options: {
          livereload: true
        }
      }
    }
  });

  // require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  // require('time-grunt')(grunt);
  grunt.loadNpmTasks('grunt-collection');

  // Test task.
  grunt.registerTask('new', ['copy:s']);

  grunt.registerTask('test-css', ['csslint']);
  grunt.registerTask('test-js', ['jshint', 'jscs']);

  // CSS distribution task.
  grunt.registerTask('dist-css', ['stylus', 'autoprefixer', 'csscomb', 'cssmin', 'usebanner','copy:css']);
  grunt.registerTask('dist-js', ['concat', 'uglify','copy:js']);

  // Default task.
  grunt.registerTask('dist', ['clean', 'dist-css', 'dist-js', 'watch']);

};
